package com.yxw.springcloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.springcloud.mapper.OrderMapper;
import com.yxw.springcloud.model.Order;
import com.yxw.springcloud.service.AccountService;
import com.yxw.springcloud.service.OrderService;
import com.yxw.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yanxw
 * @since 2021-05-10
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private StorageService storageService;

    @Autowired
    private AccountService accountService;



    @Override
    @GlobalTransactional
    public void create(Order order) {
        log.info("----->开始新建订单");
        baseMapper.insert(order);

        log.info("----->订单微服务开始调用库存，做扣减Count");
        storageService.decrease(order.getUserId(), order.getCount());
        log.info("----->订单微服务开始调用库存，做扣减end");

        log.info("----->订单微服务开始调用账户，做扣减Money");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("----->订单微服务开始调用账户，做扣减end");

        //修改订单状态，从0到1，1代表以及完成
        log.info("----->修改订单状态开始");
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("id", order.getId());
//        baseMapper.updateById(order);
        baseMapper.update(order, wrapper);
        log.info("----->修改订单状态结束");

        log.info("----->下订单结束了，O(∩_∩)O哈哈~");

    }
}
